package subject_set.interview.Day3_22_yunzi;

/**
 * @author haomin
 * @date 2023/03/22 14:37
 **/
public class Main {
    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,2,3,1,1,1,1,2,3,3,2,3};
        int post1 = 0, post2, post3 = arr.length-1;
        while (arr[post1] == 1) post1++;
        while (arr[post3] == 3) post3--;
        post2 = post1;
        while(post2 <= post3) {
            if(arr[post2] == 1) {
                swap(arr, post1, post2);
                post1++;
            }
            if(arr[post2] == 3) {
                swap(arr, post2, post3);
                post3--;
                if(arr[post2] == 1) {
                    swap(arr, post1, post2);
                    post1++;
                }
            }
            while(arr[post3] == 3) post3--;
            post2++;
        }

        for (int i : arr) {
            System.out.print(i + " ");
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}